package com.bdwm.controller;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.bdwm.dao.BorrowInfoDBService;
import com.bdwm.dao.DeviveDBService;
import com.bdwm.dao.UserDBService;
import com.bdwm.model.BorrowDtl;
import com.bdwm.model.Constants;
import com.bdwm.model.Device;
import com.bdwm.util.ParseRequestPara;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
public class BorrowInfoManageController {
	

	/**
	 * 
	 * @throws IOException 
	 * @Description：获取借用信息列表
	 * @author：wangwenhui
	 * @time：2017年11月27日下午3:04:29
	 */
	@RequestMapping(value = "/getAllBorrowDetail", method = RequestMethod.GET)
	public void getBorrowDelInfo(HttpServletResponse response,HttpServletRequest request) throws IOException {
		response.setContentType("text/html;charset=UTF-8");
		List<BorrowDtl> borrList = BorrowInfoDBService.getAllBorrowInfo();
		JSONObject json = new JSONObject();
		json.accumulate("status", 0);
		JSONArray jsonArr = new JSONArray();
		for(BorrowDtl borrowDtl: borrList) {
			JSONObject borrowDtlJson = new JSONObject();
			borrowDtlJson.accumulate("userName", borrowDtl.getUserName());
			borrowDtlJson.accumulate("deviceName", borrowDtl.getDeviceName());
			borrowDtlJson.accumulate("borrowTime", borrowDtl.getBorrowTime().toString());
			borrowDtlJson.accumulate("osType", borrowDtl.getOsType());
			borrowDtlJson.accumulate("versionType", borrowDtl.getVersionType());
			borrowDtlJson.accumulate("deviceResolution", borrowDtl.getDeviceResolution());
			borrowDtlJson.accumulate("deviceDept", borrowDtl.getDeviceDept());
			borrowDtlJson.accumulate("remark", borrowDtl.getRemark());
			jsonArr.add(borrowDtlJson);
		}
		json.accumulate("data", jsonArr);
		response.getWriter().println(json.toString());
		response.getWriter().close();
	}
	
	/**
	 * 
	 * @throws IOException 
	 * @Description：获取空闲设备信息
	 * @author：wangwenhui
	 * @time：2017年12月25日下午3:04:29
	 */
	@RequestMapping(value = "/getFreeDevices", method = RequestMethod.GET)
	public void getFreeDevice(HttpServletResponse response ,HttpServletRequest request) throws IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		JSONObject json = new JSONObject();
		String device_belongs = ParseRequestPara.parseRequestParameter(request.getParameter("deviceBelongs"));
		if(ParseRequestPara.isNull(device_belongs)) {
			json.accumulate("status", 1);
			json.accumulate("message", "device belongs is null!");
			response.getWriter().println(json.toString());
			response.getWriter().close();
			return;
		}
		List<Device> deviceList = BorrowInfoDBService.getFreeDevices(device_belongs);
		json.accumulate("status", 0);
		JSONArray jsonArr = new JSONArray();
		for(Device device: deviceList) {
			JSONObject deviceJson = new JSONObject();
			deviceJson.accumulate("deviceName",device.getDeviceName());
			deviceJson.accumulate("deviceAssetNum",device.getDeviceAssetNum());
			deviceJson.accumulate("osType",device.getOsType());
			deviceJson.accumulate("versionType",device.getVersionType());
			deviceJson.accumulate("deviceResolution",device.getDeviceResolution());
			deviceJson.accumulate("deviceBelongs",device.getDeviceBelongs());
			deviceJson.accumulate("deviceDept",device.getDeviceDept());
			deviceJson.accumulate("remark",device.getRemark());
			jsonArr.add(deviceJson);
		}
		json.accumulate("data", jsonArr);
		response.getWriter().println(json.toString());
		response.getWriter().close();
	}
	
	/**
	 *
	 * @author：changna
	 * @Description：借用设备接口
	 * @time：Mar 9, 2018，5:18:28 PM
	 *
	 */
	/**   入参举例：
	 * deviceName : 三星Galaxy Note III
	 * borrowPerson : 赵丽君
	 * borrowTime : 2018-02-02
	 * osType : Android
	 * versionType : Android 5.0
	 * deviceResolution : 1080*1020
	 * deviceDept : C端
	 * remark : QA测试使用
	 * @throws ClassNotFoundException 
	 */
	//http://localhost:8080/DeviceManage/borrowDevice?deviceName=&borrowPerson=&borrowTime=
	@RequestMapping(value = "/borrowDevice", method = RequestMethod.GET)
	public void borrowDevice(HttpServletResponse response,HttpServletRequest request) throws IOException, ClassNotFoundException{
		ServletOutputStream output = response.getOutputStream();
		request.setCharacterEncoding("utf-8");
		SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd" ); 
		BorrowDtl borrowdtl = new BorrowDtl();
		try {
			borrowdtl.setDeviceName(ParseRequestPara.parseRequestParameter(request.getParameter("deviceName")));  
			borrowdtl.setUserName(ParseRequestPara.parseRequestParameter(request.getParameter("borrowPerson"))); 
			java.util.Date utildate = format.parse(ParseRequestPara.parseRequestParameter(request.getParameter("borrowTime")));
			java.sql.Date sqldate = new java.sql.Date(utildate.getTime());
			borrowdtl.setBorrowTime(sqldate); 
//			System.out.println(" device name:" + borrowdtl.getDeviceName()+"\n user name:"+borrowdtl.getUserName() + "\n time:" + borrowdtl.getBorrowTime());
//			borrowdtl.setOsType(ParseRequestPara.parseRequestParameter(request.getParameter("osType")));
//			borrowdtl.setVersionType(ParseRequestPara.parseRequestParameter(request.getParameter("versionType")));
//			borrowdtl.setDeviceResolution(ParseRequestPara.parseRequestParameter(request.getParameter("deviceResolution")));
//			borrowdtl.setDeviceDept(ParseRequestPara.parseRequestParameter(request.getParameter("deviceDept")));
//			borrowdtl.setRemark(ParseRequestPara.parseRequestParameter(request.getParameter("remark")));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String checkPara = ParseRequestPara.checkBorrowParameter(borrowdtl);
		JSONObject json = new JSONObject();
		if (!"success".equals(checkPara)) {
			json.accumulate("status", 1);
			json.accumulate("message", checkPara);
			output.println(json.toString());
			return;
		}

		int isborrowed = BorrowInfoDBService.checkIfDeviceBorrow(borrowdtl.getDeviceName(), borrowdtl.getUserName());    //判断要借用的设备的借用状态
		if(1 == isborrowed) {
			json.accumulate("status", 1);
			json.accumulate("message", "Sorry, Device is already borrowed!");
			output.println(json.toString());
			return;
		}
		
		int status = BorrowInfoDBService.borrowDevice(borrowdtl, isborrowed);
		
		if(Constants.ERROR == status) {
			json.accumulate("status", 1);
			json.accumulate("message", "borrow failed!");
			output.println(json.toString());
			return;
		}
		json.accumulate("status", 0);
		json.accumulate("message", "borrow success!");
		output.println(json.toString());
				
	}
	
	/**
	 *
	 * @throws IOException 
	 * @author：changna
	 * @Description：借用设备接口
	 * @time：Mar 13, 2018，35:20:28 PM
	 *
	 */
	//http://localhost:8080/DeviceManage/returnDevice?deviceName=&borrowPerson=&borrowTime=
	@RequestMapping(value = "/returnDevice", method = RequestMethod.GET)
	public void returnDevice(HttpServletResponse response,HttpServletRequest request) throws IOException{
		ServletOutputStream output = response.getOutputStream();
		request.setCharacterEncoding("utf-8");
		SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd" );
		BorrowDtl borrowdtl = new BorrowDtl();
		try {
			borrowdtl.setDeviceName(ParseRequestPara.parseRequestParameter(request.getParameter("deviceName")));
			borrowdtl.setUserName(ParseRequestPara.parseRequestParameter(request.getParameter("borrowPerson")));
			java.util.Date utildate;
			utildate = format.parse(ParseRequestPara.parseRequestParameter(request.getParameter("borrowTime")));
			java.sql.Date sqldate = new java.sql.Date(utildate.getTime());
			borrowdtl.setBorrowTime(sqldate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
		String checkPara = ParseRequestPara.checkBorrowParameter(borrowdtl);  //验证borrowdtl，device_name user_name borrow_time 非空
		JSONObject json = new JSONObject();
		if (!"success".equals(checkPara)) {
			json.accumulate("status", 1);
			json.accumulate("message", checkPara);
			output.println(json.toString());
			return;
		}
		
		int isborrowed = BorrowInfoDBService.checkIfDeviceBorrow(borrowdtl.getDeviceName(), borrowdtl.getUserName());    //判断要归还设备的借用状态
		if(0 == isborrowed) {
			json.accumulate("status", 1);
			json.accumulate("message", "Device is already returned!");
			output.println(json.toString());
			return;
		}else if(-1 == isborrowed){
			json.accumulate("status", 1);
			json.accumulate("message", "sorry, Device is never borrowed or not exist, please check your list!");
			output.println(json.toString());
			return;
		}
		
		int status = BorrowInfoDBService.returnDevice(borrowdtl);
		
		if(Constants.ERROR == status) {
			json.accumulate("status", 1);
			json.accumulate("message", "return failed!");
			output.println(json.toString());
			return;
		}
		json.accumulate("status", 0);
		json.accumulate("message", "return success!");
		output.println(json.toString());
	}
}
